package cn.cjxy.controller;

import cn.cjxy.domain.Student;
import cn.cjxy.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器层
 */
@Api(value = "学生管理", tags = "学生管理相关接口")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "获取学生列表")
    @GetMapping("/list")
    public List<Student> findAll(){
        List<Student> all = studentService.findAll();
        return all;
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个学生")
    @GetMapping("/{id}")
    public Student findById(@PathVariable Long id){
       return studentService.findById(id);
    }

    /**
     * 删除单个
     * @param id
     */
    @ApiOperation(value = "根据id删除学生")
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){
        studentService.delete(id);
    }
    //修改需要传入id   而新增不需要传入id
    @ApiOperation(value = "新增或查询学生")
    @PostMapping("/save")
    public void save( @RequestBody  Student student){
        if (student.getId()!=null){//如果传过来的对象id为空 则执行新增，否则执行修改
            studentService.update(student);
        }else {
            studentService.save(student);
        }
    }

}
